//data functions
function parseNewWines(JSONData) {
	var results="";

	//TODO: use a JSON parser
	//this represents a security risk in case someone passes in JavaScript as a value, it could be interepreted maliciously 
	var data = eval('(' + JSONData + ')');

	for (var i=0;i<data.length;i++)
	{
		results = results + "<img src='/images/tri.gif'/><a class='widgetcontent' style='font-size:11;' href='/wine/edit/editWine.xqy?id="+data[i][0]+"'>" +data[i][1] + '</a><br/>'; 
	}

	return results;
}


function parseViewedWines(JSONData) {
	var results="";

	//TODO: use a JSON parser
	//this represents a security risk in case someone passes in JavaScript as a value, it could be interepreted maliciously 
	var data = eval('(' + JSONData + ')');

	for (var i=0;i<data.length;i++)
	{
		results = results + "<a class='widgetcontent' href='/wine/edit/editWine.xqy?id="+data[i][0]+"'>" +data[i][1] + '</a><br/>'; 
	}

	return results;
}

/***** BEGIN BLEND FIELDS *****/
function showBlendFields(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		 if (debug) { alert(data); }

		for (var i=0;i<data;i++)
		{ showBlendField(doc, i+1); }
		
		for (var i=1;i<data;i++) {
			var bpref = doc.getElementById("blendPercentage" + i);
			bpref.onchange = null;
		}
	}

	//get the data for a section that is in all of the entries to get the count
	countElement('doc(\''+g_id+'\')/wine', g_id, 'blend/blendPart',call);
}

	
function showBlendField (doc, rowcount) {
	//get handle to the blend div sections
	var bvar = doc.getElementById("vcol");
	var bper = doc.getElementById("bpcol");
	var delcol = doc.getElementById("delcol");
	
	//create the blend varietal input
	var cb = doc.createElement("input");
	cb.id = "blendVarietal" + rowcount;
	cb.name = cb.id;
	cb.size = "30";
	cb.type = "text";
	cb.autocomplete ="off";
	cb.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(blend/blendPart/blendVarietal)['+rowcount+']', 
						function(event) {cb.value=event;}
				);
	cb.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(blend/blendPart/blendVarietal)['+rowcount+']', this.value);
	}
	
	//add the blend varietal input
	vcol.appendChild(cb); // add the varietal input to the td
	vcol.appendChild(doc.createElement("br"));

	//create a blend percentage input
	var it = doc.createElement("input");
	it.type = "text";
	it.id = "blendPercentage" + rowcount;
	it.name = it.id;
	it.size = "6";
	it.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(blend/blendPart/blendPercentage)['+rowcount+']',
						function(event) {it.value = event;} );
	
	it.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(blend/blendPart/blendPercentage)['+rowcount+']', this.value);
	}

	//add the blend percentage input
	bper.appendChild(it); // add the percent input to the td
	bper.appendChild(doc.createElement("br"));

	
	//create a delete control percentage input
	var del = doc.createElement("img");
	//del.appendChild(doc.createTextNode("del"));
	delcol.appendChild(del); // add the del control to the td
	del.src = "/porvino/images/cancel_off.png";
	del.onclick = function (event) {
			deleteBlendField(doc, rowcount);
		}
	del.id = "blenddel" + rowcount;
	del.name = del.id;
	del.value = "blenddel" + rowcount;
	delcol.appendChild(doc.createElement("br"));

}	

function newBlendField(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		 var debug = false;
		 if (debug) { "newBlendField count data: " + alert(data); }

	//<inventoryEntry>...
	//add the blank entry to the XML document
	if (debug) { alert("calling insertElement to add value to document"); }
	insertElement('doc(\''+g_id+'\')/wine[@id%3d\''+ g_id +'\']/blend', 
		'<blendPart dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""><blendVarietal dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><blendPercentage dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/></blendPart>');
	
	if (debug) { alert("calling updateAttribute to update the document status"); }
	updateAttribute('doc(\''+g_id+'\')/wine', g_id,   "", "dateModified" , "currentdatetime")

		showBlendField(doc, parseInt(data) + 1); 
	}	

	countElement('doc(\''+g_id+'\')/wine', g_id, 'blend/blendPart',call);
}

function deleteBlendField (doc, row) {
	//delete current field
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		//alert("deleteBlendField anon return fnctn called");
		//remove display of blend fields
		
		var vvar = doc.getElementById("vcol").childNodes;
		var bpvar = doc.getElementById("bpcol").childNodes;
		var delvar = doc.getElementById("delcol").childNodes;
		
	
		//brute force removal
		//alert (vvar.length);
		//count down. otherwise the array is shortened each time and it leaves elements on screen
		
		for (var i=vvar.length-1; i>=0; i--) {
			//alert("removing child " + i);
			//alert (i + " " + vvar[i]);
			vvar[i].parentNode.removeChild(vvar[i]);
		}
		for (var i=bpvar.length-1;i>=0; i--) {
			//alert("removing child " + i);
			bpvar[i].parentNode.removeChild(bpvar[i]);
		}
		for (var i=delvar.length-1; i>=0; i--) {
			//alert("removing child " + i);
			delvar[i].parentNode.removeChild(delvar[i]);
		}
		
		//redisplay blends from scratch
		showBlendFields(doc);
	}
	
	//alert("deleteBlendField calling fnctn delete element");
	deleteElement('doc(\''+g_id+'\')/wine[@id%3d\''+ g_id +'\']/blend/blendPart['+ row +']','', call);
}
/* END BLEND FIELDS */


function showInventoryFields(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		 if (debug) { alert(data); }
	
		//TODO: use a JSON parser
		//this represents a security risk in case someone passes in JavaScript as a value, it could be interepreted maliciously 
		var da = eval('(' + data + ')');
		for (var i=0;i<da.length;i++)
		{ showInventoryField(doc, i+1); 
		}
		for (var i=1;i<da.length;i++) {
				var bpref = doc.getElementById("quantity" + i);
				bpref.onchange = null;
			}
	}

	//get the data for a section that is in all of the entries to get the count
	getValuesJSON('doc(\''+g_id+'\')/wine', g_id, 'inventory/inventoryEntry/quantity',call);
}

//TODO standardize the text field creation and the checkbox creation
function showInventoryField (doc, rowcount) {
	//get handle to the blend div sections
	var onhandcol = doc.getElementById("onhandcol");
	var qtycol = doc.getElementById("qtycol");
	var locationcol = doc.getElementById("locationcol");
	var costcol = doc.getElementById("costcol");
	var datecol = doc.getElementById("datecol");
	var futurecol = doc.getElementById("futurecol");
	var wishcol = doc.getElementById("wishcol");
	var invdelcol = doc.getElementById("invdelcol");
	
	//create the onhand input
	var oh = doc.createElement("input");
	oh.id = "onhand" + rowcount;
	oh.name = oh.id;
	oh.size= "2"
	oh.type = "text";
	oh.autocomplete ="off";
	oh.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/onhand)['+rowcount+']', 
						function(event) {oh.value=event;}
				);
	oh.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/onhand)['+rowcount+']', this.value);
	}
	//add the blend varietal input
	onhandcol.appendChild(oh); // add the onhand input to the td
	
	//create the qty input
	var qty = doc.createElement("input");
	qty.id = "quantity" + rowcount;
	qty.name = qty.id;
	qty.size= "2"
	qty.type = "text";
	qty.autocomplete ="off";
	qty.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/quantity)['+rowcount+']', 
						function(event) {qty.value=event;}
				);
	qty.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/quantity)['+rowcount+']', this.value);
	}
	//add the blend varietal input
	qtycol.appendChild(qty); // add the qty input to the td	
	
	//create the location input
	var loc = doc.createElement("input");
	loc.id = "location" + rowcount;
	loc.name = loc.id;
	loc.size= "7"
	loc.type = "text";
	loc.autocomplete ="on";
	loc.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/location)['+rowcount+']', 
						function(event) {loc.value=event;}
				);
	loc.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/location)['+rowcount+']', this.value);
	}
	//add the blend varietal input
	locationcol.appendChild(loc); // add the loc input to the td	
	
	//create the size input
	var size = doc.createElement("input");
	size.id = "size" + rowcount;
	size.name = size.id;
	size.size= "7"
	size.type = "text";
	size.autocomplete ="on";
	size.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/size)['+rowcount+']', 
						function(event) {size.value=event;}
				);
	size.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/size)['+rowcount+']', this.value);
	}
	//add the blend varietal input
	sizecol.appendChild(size); // add the size input to the td	


	//create the cost input
	var cost = doc.createElement("input");
	cost.id = "cost" + rowcount;
	cost.name = cost.id;
	cost.size= "7"
	cost.type = "text";
	cost.autocomplete ="on";
	cost.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/cost)['+rowcount+']', 
						function(event) {cost.value=event;}
				);
	cost.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/cost)['+rowcount+']', this.value);
	}
	//add the blend varietal input
	costcol.appendChild(cost); // add the cost input to the td
	
	//create the date input
	var date = doc.createElement("input");
	date.id = "date" + rowcount;
	date.name = date.id;
	date.size= "7"
	date.type = "text";
	date.autocomplete ="on";
	date.value = getValue('doc(\''+g_id+'\')/wine',g_id, '(inventory/inventoryEntry/date)['+rowcount+']', 
						function(event) {date.value=event;}
				);
	date.onblur = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/date)['+rowcount+']', this.value);
	}
	datecol.appendChild(date); // add the date input to the td
	
	
	//create the future input
	var future = doc.createElement("input");
	future.id = "future" + rowcount;
	future.name = future.id;
	future.type = "checkbox";
	future.value = getValue('doc(\'' + g_id + '\')/wine',g_id, '(inventory/inventoryEntry/future)['+rowcount+']',function (e) {if (e == "true"){ future.checked = e; future.value= e}});
				
	future.onchange = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/future)['+rowcount+']', this.checked);
	}
	futurecol.appendChild(future); // add the future input to the td
	futurecol.appendChild( doc.createElement("br") );
	

	//create the wish input
	var wish = doc.createElement("input");
	wish.id = "wish" + rowcount;
	wish.name = wish.id;
	wish.type = "checkbox";
	wish.value = getValue('doc(\'' + g_id + '\')/wine',g_id, '(inventory/inventoryEntry/wish)['+rowcount+']',function (e) {if (e == "true"){ wish.checked = e; wish.value= e}});
				
	wish.onchange = function(event) {
		updateElementText('doc(\''+g_id+'\')/wine', g_id, '(inventory/inventoryEntry/wish)['+rowcount+']', this.checked);
	}
	wishcol.appendChild(wish); // add the wish input to the td
	wishcol.appendChild( doc.createElement("br") );
	
	//create the delete input
	//create a delete control percentage input
	var del = doc.createElement("img");
	invdelcol.appendChild(del); // add the del control to the td
	del.src = "/porvino/images/cancel_off.png";
	del.onclick = function (event) {
			deleteInventoryField(doc, rowcount);
		}
	del.id = "invdel" + rowcount;
	del.name = del.id;
	del.value = "invdel" + rowcount;
	invdelcol.appendChild(doc.createElement("br"));
}	



function newInventoryField(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		debug = false;
		if (debug) { "calling newInventoryField " + alert(data); }
		
		//<inventoryEntry>...
		//add the blank entry to the XML document
		if (debug) { alert("calling insertElement "); }
		insertElement('doc(\''+g_id+'\')/wine[@id%3d\''+ g_id +'\']/inventory', 
			'<inventoryEntry dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""><onhand dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><quantity dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><location dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><size dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><cost dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><date dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><future dateCreated%3d"{fn:current-dateTime()}" dateModified%3d"">false</future><wish dateCreated%3d"{fn:current-dateTime()}" dateModified%3d"">false</wish></inventoryEntry>');
		
		if (debug) { alert("calling updateAttribute "); }
		updateAttribute('doc(\''+g_id+'\')/wine', g_id,   "", "dateModified" , "currentdatetime")
		
		showInventoryField(doc, parseInt(data) + 1); 
	}



	//count the number of entries in the database
	countElement('doc(\''+g_id+'\')/wine', g_id, 'inventory/inventoryEntry',call);
}

function deleteInventoryField (doc, row) {
//the function that the query will bind to and then call when the results have returned
	function call(data) {
		//alert("deleteInvField anon return fnctn called");
		//remove display of inv fields
		
		var ac = doc.getElementById("onhandcol").childNodes;
		var bc = doc.getElementById("qtycol").childNodes;
		var cc = doc.getElementById("locationcol").childNodes;
		var dc = doc.getElementById("sizecol").childNodes;
		var ec = doc.getElementById("costcol").childNodes;
		var fc = doc.getElementById("datecol").childNodes;
		var gc = doc.getElementById("futurecol").childNodes;
		var hc = doc.getElementById("wishcol").childNodes;
		var ic = doc.getElementById("invdelcol").childNodes;
	
	
		//brute force removal
		//alert (vvar.length);
		//count down. otherwise the array is shortened each time and it leaves elements on screen
		
		for (var i=ac.length-1; i>=0; i--) {
			ac[i].parentNode.removeChild(ac[i]);
			bc[i].parentNode.removeChild(bc[i]);
			cc[i].parentNode.removeChild(cc[i]);
			dc[i].parentNode.removeChild(dc[i]);
			ec[i].parentNode.removeChild(ec[i]);
			fc[i].parentNode.removeChild(fc[i]);
		}
		for (var i=gc.length-1; i>=0; i--) {
			gc[i].parentNode.removeChild(gc[i]);
			hc[i].parentNode.removeChild(hc[i]);
			ic[i].parentNode.removeChild(ic[i]);
		}

		
		//redisplay inventory from scratch
		showInventoryFields(doc);
	}
	
	//alert("deleteInventoryField calling fnctn delete element");
	deleteElement('doc(\''+g_id+'\')/wine[@id%3d\''+ g_id +'\']/inventory/inventoryEntry['+ row +']','', call);
	


}
/* END INVENTORY FIELDS */


/***** TASTING FIELDS *****/
function newTastingField(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		debug = false;
		 if (debug) { alert(data); }
	
		//TODO: use a JSON parser
		//this represents a security risk in case someone passes in JavaScript as a value, it could be interepreted maliciously 
		var da = eval('(' + data + ')');
		if (debug) { alert("calling newTastingField " + (da.length) ); }
		showNote(doc, da.length); 
	}

function call1(data) {

	if (debug) { alert("calling updateAttribute "); }
	updateAttribute('doc(\''+g_id+'\')/wine', g_id,   "", "dateModified" , "currentdatetime")

	//get the data for a section that is in all of the entries to get the count
	getValuesJSON('doc(\''+g_id+'\')/wine', g_id, 'notes/note/date',call);

}
	//<note><date/><taster/><score/><notes/></note>
	//add the blank entry to the XML document
	if (debug) { alert("calling insertElement "); }
	var temp = insertElement('doc(\''+g_id+'\')/wine[@id%3d\''+ g_id +'\']/notes', 
		'<note dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""><date dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><taster dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><score dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/><notes dateCreated%3d"{fn:current-dateTime()}" dateModified%3d""/></note>',
		call1);
	

}


function showTastingFields(doc) {
	var debug = false;
	
	//the function that the query will bind to and then call when the results have returned
	function call(data) {
		debug = false;
		 if (debug) { alert(data); }
	
		//TODO: use a JSON parser
		//this represents a security risk in case someone passes in JavaScript as a value, it could be interepreted maliciously 
		var da = eval('(' + data + ')');
		for (var i=0;i<da.length;i++)
		{ 
			if (debug) { alert("calling showTastingField " + (i+1) ); }
			showNote(doc, i+1); 
		}
		for (var i=1;i<da.length;i++) {
				//var bpref = doc.getElementById("blendPercentage" + i);
				//bpref.onchange = null;
			}
	}

	//get the data for a secion that is in all of the entries to get teh count
	getValuesJSON('doc(\''+g_id+'\')/wine', g_id, 'notes/note/date',call);
}

function showNote(doc, rowcount) {
	var debug = false;
	//get handle to the blend div sections
	var tn = doc.getElementById("tastingNotes");
	
	//add spacing table entry
	var tr1 = doc.createElement("tr");
	tr1.colspan="3";
	var tr1_td = doc.createElement("td");
	tr1.appendChild(tr1_td);
	tn.appendChild(tr1);

	//add the labels
	var tr2 = doc.createElement("tr");
	tr2.valign='top';
	tr2.border='0';
	//tr2.setAttribute("onMouseOver","addWorld(document,document.getElementById('td_score'))");
	//tr2.setAttribute("onMouseOut","removeWorld(document,document.getElementById('td_score'))");

	var tds1 = doc.createElement("td");
	tds1.setAttribute("bgcolor", 'CCCCCC');
	tds1.appendChild(document.createTextNode("date"));
	var tds2 = doc.createElement("td");
	tds2.setAttribute("bgcolor", 'CCCCCC');
	tds2.appendChild(document.createTextNode("taster"));
	var tds3 = doc.createElement("td");
	tds3.setAttribute("bgcolor", 'CCCCCC');
	tds3.setAttribute("id", 'td_score');
	tds3.appendChild(document.createTextNode("score"));
		
	//tr2.appendChild(td);
	tr2.appendChild(tds1);
	tr2.appendChild(tds2);
	tr2.appendChild(tds3);
	tn.appendChild(tr2);
	

	//add the date field
	var tr_date = doc.createElement("tr");
	var td_date = doc.createElement("td");
	td_date.setAttribute("bgcolor", 'CCCCCC');
	//var td_date1 = doc.createElement("td");

	var in_date = doc.createElement("input");
	in_date.setAttribute("class","tastingNotes");
	in_date.setAttribute("type","text");
	in_date.setAttribute("name","tn_date"+rowcount);
	in_date.setAttribute("id","tn_date"+rowcount);
	in_date.setAttribute("size","8");
	var in_date_onchange = "updateElementText('doc(\\'' + g_id +'\\')/wine', g_id, '(notes/note/date)["+rowcount+"]', encodeURI(this.value));"
	if (debug){alert ("creating in_date_onchange: " + in_date_onchange);}
	in_date.setAttribute("onChange",in_date_onchange);

	var in_date_script = doc.createElement("script");
	//eliminated cdata protection <![CDATA[  bbb  ]]> 
	var in_date_script_text = " getValue('doc(\\'' + g_id +'\\')/wine',g_id, '(notes/note/date)["+rowcount+"]', function (e) {document.getElementById(\'tn_date"+rowcount+"\').value=e;});"
	if (debug){alert ("creating in_date_script : " + in_date_script_text);}
	in_date_script.appendChild(document.createTextNode(in_date_script_text));


	td_date.appendChild(in_date);
	td_date.appendChild(in_date_script);
	//tr_date.appendChild(td_date1);
	tr_date.appendChild(td_date);

	//add the taster field
	var td_taste = doc.createElement("td");
	td_taste.setAttribute("bgcolor", 'CCCCCC');

	var in_taste = doc.createElement("input");
	in_taste.setAttribute("type","text");
	in_taste.setAttribute("name","tn_taster"+rowcount);
	in_taste.setAttribute("id","tn_taster"+rowcount);
	in_taste.setAttribute("size","20");
	var in_taste_onchange = "updateElementText('doc(\\'' + g_id +'\\')/wine', g_id, '(notes/note/taster)["+rowcount+"]', encodeURI(this.value));"
	in_taste.setAttribute("onChange",in_taste_onchange);

	var in_taste_script = doc.createElement("script");
	//eliminated cdata protection <![CDATA[  bbb  ]]> 
	var in_taste_script_text = " getValue('doc(\\'' + g_id +'\\')/wine',g_id, '(notes/note/taster)["+rowcount+"]', function (e) {document.getElementById(\'tn_taster"+rowcount+"\').value=e;});"
	in_taste_script.appendChild(document.createTextNode(in_taste_script_text));

	td_taste.appendChild(in_taste);
	td_taste.appendChild(in_taste_script);
	tr_date.appendChild(td_taste);
	
	
	//add the score field
	var td_score = doc.createElement("td");
	td_score.setAttribute("bgcolor", 'CCCCCC');

	var in_score = doc.createElement("input");
	in_score.setAttribute("type","text");
	in_score.setAttribute("name","tn_score"+rowcount);
	in_score.setAttribute("id","tn_score"+rowcount);
	in_score.setAttribute("size","4");
	var in_score_onchange = "updateElementText('doc(\\'' + g_id +'\\')/wine', g_id, '(notes/note/score)["+rowcount+"]', encodeURI(this.value));"
	in_score.setAttribute("onChange",in_score_onchange);

	var in_score_script = doc.createElement("script");
	//eliminated cdata protection <![CDATA[  bbb  ]]> 
	var in_score_script_text = " getValue('doc(\\'' + g_id +'\\')/wine',g_id, '(notes/note/score)["+rowcount+"]', function (e) {document.getElementById(\'tn_score"+rowcount+"\').value=e;});"
	in_score_script.appendChild(document.createTextNode(in_score_script_text));

	td_score.appendChild(in_score);
	td_score.appendChild(in_score_script);
	tr_date.appendChild(td_score);
	
	tn.appendChild(tr_date); // adding the tr date -->
	
	//create the spacing for notes area
	var tr_space = doc.createElement("tr");
	//var td_space = doc.createElement("td");
	var td_space_3 = doc.createElement("td");
	td_space_3.setAttribute("colspan","3");
	td_space_3.setAttribute("bgcolor","CCCCCC");

	//tr_space.appendChild(td_space);
	tr_space.appendChild(td_space_3);
	tn.appendChild(tr_space);
	
	//create the text notes area
	var tr_notes = doc.createElement("tr");
	//var td_notes = doc.createElement("td");
	var td_notes3 = doc.createElement("td");
	td_notes3.setAttribute("colspan","3");
	td_notes3.setAttribute("bgcolor","CCCCCC");

	//defines the text area for a wine
	var td_text = doc.createElement("textarea");
	//td_text.setAttribute("cols","80");
	td_text.setAttribute("rows","5");
	td_text.setAttribute("name","tn_notes"+rowcount);
	td_text.setAttribute("id","tn_notes"+rowcount);
	var text_onchange = "updateElementText('doc(\\'' + g_id +'\\')/wine', g_id, '(notes/note/notes)["+rowcount+"]', encodeURI(escape(this.value)));"
	td_text.setAttribute("onChange",text_onchange);

	var text_script = doc.createElement("script");
	//eliminated cdata protection <![CDATA[  bbb  ]]> 
	var text_script_text = " getValue('doc(\\'' + g_id +'\\')/wine',g_id, '(notes/note/notes)["+rowcount+"]', function (e) {document.getElementById(\'tn_notes"+rowcount+"\').value=unescape(e);});"
	text_script.appendChild(document.createTextNode(text_script_text));

	td_notes3.appendChild(td_text);
	td_notes3.appendChild(text_script);
	//tr_notes.appendChild(td_notes);
	tr_notes.appendChild(td_notes3);
	
	tn.appendChild(tr_notes);

}
